//
//  ViewController.m
//  readNovels
//
//  Created by wulanzhou on 16/8/11.
//  Copyright © 2016年 wulanzhou. All rights reserved.
//

#import "ViewController.h"
#import "BookDBService.h"
#import "CatalogDBService.h"
#import "ChapterDBService.h"
#import "CacheDataUtil.h"
#import "BookDir.h"

@interface ViewController ()

@property (nonatomic,strong) BookDBService *bookDBService;
@property (nonatomic,strong) CatalogDBService *catalogDBService;
@property (nonatomic,strong) ChapterDBService *chapterDBService;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //https://www.630kan.com/book/20894/
    
    //1.添加书籍
    //NSInteger bid=[BookDBService addBookName:@"天神决" author:@"太一生水"];
    //NSLog(@"bid = %ld",bid);
    
    //2.下载目录

    /**
    [self.catalogDBService downloadCatalogWithUrl:@"https://www.630kan.com/book/20894/" xpath:@"//dd[@class='col-md-3']/a" encoding:@"GBK" loop:^(Catalog *catalog, TFHppleElement *element) {
        
        catalog.page_url=[NSString stringWithFormat:@"http://www.630kan.com%@",[element objectForKey:@"href"]];
        
    } completed:^(NSArray<Catalog *> *catalogs, NSError *error) {
        
        if (catalogs && [catalogs count]>0) {
             [self.catalogDBService addCatalogs:catalogs bookId:1];
             NSLog(@"目录下载完成!!!!");
        }
    }];
**/
    

    /**
    //3.下载章节
    ChapterHpple *chapterHpple = [[ChapterHpple alloc] init];
    //chapterHpple.encoding = @"GBK";
    chapterHpple.encoding = @"utf-8";
    chapterHpple.xpath = @"//div[@id='htmlContent']";
    chapterHpple.childXPathQuery = @"//a[@id='linkNext']";
    chapterHpple.loopChapterBlock = ^(Catalog * _Nonnull catalog, TFHppleElement * _Nonnull element) {
      
        catalog.page_url = [NSString stringWithFormat:@"http://www.630kan.com/book/20894/%@",[element.attributes objectForKey:@"href"]];
    };
    [self.chapterDBService downloadChaptersWithBookId:1 hpple:chapterHpple];

    ***/
    
    /**
    //4.合成完整章节
       NSArray<Chapter *> *source = [self.chapterDBService chapterWithBookId:1];
       if (source && [source count]>0) {
          [[BookDir shareInstance] compressWithChpaters:source fileName:@"天神决.txt"];
       }
    ***/
    
  
    
   
    
    
}




- (BookDBService *)bookDBService {
    
    if (!_bookDBService) {
        _bookDBService=[[BookDBService alloc] init];
    }
    return _bookDBService;
}

- (CatalogDBService *)catalogDBService {
    
    if (!_catalogDBService) {
        _catalogDBService=[[CatalogDBService alloc] init];
    }
    return _catalogDBService;
}

- (ChapterDBService *)chapterDBService {
    
    if (!_chapterDBService) {
        _chapterDBService=[[ChapterDBService alloc] init];
    }
    return _chapterDBService;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
